package cn.kgc.fcm.dsbank.config;

import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Component
@EnableOpenApi
//读取配置文件中前缀为swagger的内容
@ConfigurationProperties("swagger")
@Data
public class SwaggerConfiguration {


  //是否开启swagger，⽣产环境⼀般关闭，所以这⾥定义⼀个变量
  private Boolean enable;

  /**
	 *  项⽬应⽤名
	 */
  private String applicationName;

  /**
	 * 项⽬版本信息
	 */
  private String applicationVersion;

  /**
	 * 项⽬描述信息
	 */
  private String applicationDescription;

  @Bean
  public Docket docket(){


    return new Docket(DocumentationType.OAS_30)

      .pathMapping("/")

      // 定义是否开启swagger，false为关闭，可以通过变量控制，线上关闭
      .enable(enable)

      //配置api文档元信息
      .apiInfo(apiInfo())

      // 选择哪些接口作为swagger的doc发布
      .select()

      //apis() 控制哪些接口暴露给swagger，
      // RequestHandlerSelectors.any() 所有都暴露
      // RequestHandlerSelectors.basePackage("org.christopher.*")  指定包位置
      // withMethodAnnotation(ApiOperation.class)标记有这个注解 ApiOperation
      .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

      .paths(PathSelectors.any())

      .build();

  }


  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title(applicationName)
      .description(applicationDescription)
      .contact(new Contact("大神班", "http://www.dsbank.com", "cy.liu@dsbank.com"))
      .version(applicationVersion)
      .build();
  }

}